package leetcode.code1737;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int minCharacters(String a, String b) {
		int[] dic1 = this.dic(a);
		int[] dic2 = this.dic(b);
		int[] sums1 = this.sums(dic1);
		int[] sums2 = this.sums(dic2);
		int ans = Integer.MAX_VALUE;
		for (int i = 0; i < 25; i++) {
			// 变成同一个数i
			int ans1 = sums1[26] - dic1[i] + sums2[26] - dic2[i];
			// a 小
			int ans2 = sums1[26] - sums1[i + 1] + sums2[i + 1];
			// b 小
			int ans3 = sums2[26] - sums2[i + 1] + sums1[i + 1];
			ans = Math.min(Math.min(ans2, ans), Math.min(ans1, ans3));
		}
		ans = Math.min(ans, sums1[26] - dic1[25] + sums2[26] - dic2[25]);
		return ans;
	}

	private int[] sums(int[] dic1) {
		int[] ans = new int[27];
		for (int i = 1; i < ans.length; i++) {
			ans[i] = ans[i - 1] + dic1[i - 1];
		}
		return ans;
	}

	private int[] dic(String a) {
		int[] ans = new int[26];
		for (int i = 0; i < a.length(); i++) {
			ans[a.charAt(i) - 'a']++;
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(2, this.minCharacters("a", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
		H.compare(2, this.minCharacters("aba", "caa"));
		H.compare(3, this.minCharacters("dabadd", "cda"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
